package com.fh.Collection;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/*
1. 创建3个Dog{name。age}对象，放入到ArrayList中，赋给List引用
2. 用迭代器和增强for循环两种方式来遍历
3. 重写Dog的toString方法，输出name和age
 */
public class collectionExercise {
    @SuppressWarnings({"all"})
    public static void main(String[] args) {
        List list = new ArrayList();
        list.add(new Dog("小黄",5));
        list.add(new Dog("小白",7));
        //使用for循环
        for (Object dog :list) {
            System.out.println(dog);
        }
        //使用迭代器
        System.out.println("=====使用迭代器======");
        Iterator iterator = list.iterator();
        while (iterator.hasNext()) {
            Object obj = iterator.next();
            System.out.println(obj);
        }
    }
}
class Dog{
    String name;
    int age;
    public Dog(String name, int age) {
        this.name = name;
        this.age = age;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Dog{" +
                "age=" + age +
                ", name='" + name + '\'' +
                '}';
    }
}
